/**
 * 根据json配置字段禁用与否及必填校验与否 需配合mixin_approval_edit使用
 */
export const mixin_form_config = {
  mixins: [],
  data() {
    return {
      /**
			 * 表单必填 禁用状态配置json
			 * 格式 {
			 * prop：{
			 *  (禁用状态)diabled:false,
			 *  (必填校验）required:true,
			 *  (最高字数)maxlen:800
			 * }
			 * }
			 */
      formItemJson: {},
      // 定义在编码中 作为不同配置区分
      formCode: 'edit',
      // dataCodeList接口中表单配置编码的名称
      configListName: 'formConfigList'
    }
  },
  watch: {
    codeModel(newV, oldV) {
      let formConfig = newV[this.configListName]
      if (formConfig !== void 0 && formConfig.length > 0) {
        this.formItemJson = JSON.parse(formConfig.find(e => e.code === this.formCode).description)
      }
    },
    formCode(newV, oldV) {
      let formConfig = this.codeModel[this.configListName]
      if (formConfig !== void 0 && formConfig.length > 0 && newV) {
        this.formItemJson = JSON.parse(formConfig.find(e => e.code === newV).description)
      }
    },
    // 获取审批formCode参数
    approvalNodeModel(newV, oldV) {
      let param = newV.approvalparam
      if (param) {
        this.formCode = JSON.parse(param)['formCode']
      }
    }
  },
  computed: {
    // 处理禁用状态
    disabledFormItem() {
      return function(prop) {
        let obj = this.formItemJson
        return Object.keys(obj).filter(e => e === prop && obj[e].disabled === true).length > 0
      }
    },
    // 处理必填校验
    requireFormItem() {
      return function(prop) {
        let obj = this.formItemJson
        let exist = Object.keys(obj).filter(e => e === prop && obj[e].required === true).length > 0
        if (exist) {
          return this.rules.validateRequire
        } else {
          return []
        }
      }
    },
    // 处理最多字数
    maxLengthFormItem() {
      return function(prop) {
        let obj = this.formItemJson
        let exist = Object.keys(obj).filter(e => e === prop).length > 0
        if (exist) {
          return obj[prop].maxlen
        } else {
          return 100
        }
      }
    }
  }
}
